﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Account;
using Volo.Abp.AutoMapper;
using Volo.Abp.BlobStoring;
using Volo.Abp.BlobStoring.Aliyun;
// using Volo.Abp.EventBus.RabbitMq;
using Volo.Abp.FeatureManagement;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
using WMSSystem.Application.Contracts.Workflow;
using WMSSystem.Application.Workflow;
using WorkflowCore.Interface;

namespace WMSSystem;

[DependsOn(
    typeof(WMSSystemDomainModule),
    typeof(AbpAccountApplicationModule),
    typeof(WMSSystemApplicationContractsModule),
    typeof(AbpIdentityApplicationModule),
    typeof(AbpPermissionManagementApplicationModule),
    typeof(AbpTenantManagementApplicationModule),
    typeof(AbpFeatureManagementApplicationModule),
    typeof(AbpSettingManagementApplicationModule)
    // typeof(AbpEventBusRabbitMqModule)
    
    )]
public class WMSSystemApplicationModule : AbpModule
{   
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<WMSSystemApplicationModule>();
        });

        // 注册工作流定义
        context.Services.AddTransient<InboundApprovalWorkflow>();
        
        // 注册用户角色服务
        context.Services.AddTransient<UserRoleService>();
        
        // 注册工作流服务
        context.Services.AddTransient<WorkflowService>();
    }

    public override void OnApplicationInitialization(Volo.Abp.ApplicationInitializationContext context)
    {
        // 在应用启动时注册工作流定义
        var workflowHost = context.ServiceProvider.GetService<WorkflowCore.Interface.IWorkflowHost>();
        if (workflowHost != null)
        {
            try
            {
                workflowHost.RegisterWorkflow<InboundApprovalWorkflow, InboundApprovalData>();
                Console.WriteLine("✅ 工作流定义注册成功: InboundApprovalWorkflow");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ 工作流定义注册失败: {ex.Message}");
            }
        }
        else
        {
            Console.WriteLine("❌ WorkflowHost 服务未找到");
        }
    }
}
